package shujia.day11;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/*
    "aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)

 */
public class MapTest1 {
    public static void main(String[] args) {
        String s1 = "aababcabcdabcde";

        TreeMap<Character, Integer> map1 = new TreeMap<>();

        char[] chars = s1.toCharArray();
        for (char aChar : chars) {
            //判断集合集合是否有该键
            if(map1.containsKey(aChar)){
               map1.put(aChar, map1.get(aChar)+1);
            }else {
                map1.put(aChar, 1);
            }
        }

        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<Character, Integer>> entries = map1.entrySet();
        for (Map.Entry<Character, Integer> entry : entries) {
            Character c = entry.getKey();
            Integer counts = entry.getValue();
            sb.append(c).append("(").append(counts).append(")");
        }

        String res = sb.toString();
        System.out.println(res);

    }
}
